﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class GetHtml : UserControl
    {
        public GetHtml()
        {
            InitializeComponent();
        }

        public List<string> list = new List<string>();

        private void GetHtml_Load(object sender, EventArgs e)
        {
            try
            {
                this.g_problemTypeTableAdapter.运算集(this.guestDataSet.G_problemType);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage4;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage3;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage5;

            string zhi1 = textBox3.Text;
            string zhi2 = textBox5.Text;
            //string yuensuan = textBox2.Text;
            string[] list1 = null;
            string[] list2 = null;

            if (checkBox1.Checked)
            {
                list1 = zhi1.Split(Environment.NewLine.ToCharArray());
                list2 = zhi2.Split(Environment.NewLine.ToCharArray());
            }
            else
            {
                char fenge = char.Parse(textBox8.Text);
                list1 = zhi1.Split(fenge);
                list2 = zhi2.Split(fenge);
            }
            list.Clear();
            string jieguo = "";
            int iint = 0;

            for (int i = 0; i < list1.Length; i++)
            {
                string yunsuan = "";
                if (!string.IsNullOrWhiteSpace(list1[i]))
                {

                    for (int j = 0; j < panel1.Controls.Count; j++)
                    {
                        if (panel1.Controls[j].Name == "text" + iint.ToString())
                        {
                            TextBox dg = (TextBox)(panel1.Controls[j]);
                            yunsuan = dg.Text;
                            break;
                        }
                    }
                    string mydan = GetStringmyjieguo(list1[i], list2[i], yunsuan, iint);
                    jieguo += mydan;
                    list.Add(mydan);

                    iint++;
                }
            }

            textBox1.Text = jieguo;

        }

        public string GetStringmyjieguo(string zhi1, string zhi2, string dan, int iint)
        {
            int zizeng = int.Parse(textBox7.Text);

            dan = dan.Replace("[val]", zhi1);
            dan = dan.Replace("[val1]", zhi1);

            dan = dan.Replace("[val2]", zhi2);

            dan = dan.Replace("[zint]", (zizeng + iint).ToString());

            return dan;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage4;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
                Clipboard.SetDataObject(textBox1.Text);
        }
        //集合1 label11111
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            Getzifu();
        }
        //集合2  label9
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            Getzifu();
        }

        public void Getzifu()
        {
            string zhi = textBox3.Text;
            string zi = textBox5.Text;

            string[] ziList = { };
            string[] zhiList = { };

            if (checkBox1.Checked)
            {
                ziList = zhi.Split(Environment.NewLine.ToCharArray());
                zhiList = zi.Split(Environment.NewLine.ToCharArray());
            }
            else
            {
                ziList = zhi.Split(Char.Parse(textBox8.Text));
                zhiList = zi.Split(Char.Parse(textBox8.Text));
            }

            int k1 = 0; int k2 = 0;
            foreach (string item in zhiList)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    k1++;
                }
            }
            foreach (string item in ziList)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    k2++;
                }
            }

            label9.Text = k1.ToString() + "/" + (zhiList.Length / 2 + 1);
            label11111.Text = k2.ToString() + "/" + (ziList.Length / 2 + 1);
        }



        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void SetComboBox(ComboBox ComboBox1, TextBox TextBox)
        {
            if (comboBox1.SelectedValue == null) { return; }
            Mydb mydb = new Mydb();

            DataTable dt = mydb.getG_Fault2(comboBox1.SelectedValue.ToString());

            if (dt.Rows.Count != 0)
            {
                TextBox.Text = Mydb.nachu(dt.Rows[0]["G_Fault"].ToString());
            }

            ComboBox1.DataSource = dt;
            ComboBox1.DisplayMember = "G_Name";
            ComboBox1.ValueMember = "G_id";

        }

        public List<string> GetString(string[] mylist)
        {
            List<string> str = new List<string>();

            foreach (var item in mylist)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    str.Add(item);
                }
            }
            return str;
        }
        public static bool zhengzai = false;
        private void comboBox1_SelectedIndexChangedMy(object sender, EventArgs e)
        {
            var mysender = sender as ComboBox;
            string textcon = "";
            string myid = mysender.SelectedValue.ToString();

            Mydb mydb = new Mydb();

            DataTable dt = mydb.getG_Fault(myid);

            if (dt.Rows.Count != 0)
            {
                textcon = dt.Rows[0]["G_Fault"].ToString();
            }

            for (int i = 0; i < panel1.Controls.Count; i++)
            {
                if (panel1.Controls[i].Name == "text" + mysender.Tag.ToString())
                {
                    TextBox dg = (TextBox)(panel1.Controls[i]);
                    if (!zhengzai)
                    {
                        dg.Text = Mydb.nachu(textcon);
                    }
                }
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            zhengzai = true;
            for (int j = 0; j < panel1.Controls.Count; j++)
            {
                if (panel1.Controls[j] is ComboBox)
                {
                    ComboBox dg = (ComboBox)(panel1.Controls[j]);
                    //dg.Items.Clear();


                    if (comboBox1.SelectedValue == null) { return; }
                    Mydb mydb = new Mydb();

                    DataTable dt = mydb.getG_Fault2(comboBox1.SelectedValue.ToString());

                    dg.DataSource = dt;
                    dg.DisplayMember = "G_Name";
                    dg.ValueMember = "G_id";


                }
            }
            zhengzai = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {

            this.tabControl1.SelectedTab = this.tabPage4;
            string zhi = textBox3.Text;
            string zi = textBox5.Text;
            panel1.Controls.Clear();
            List<string> ziList = new List<string>();

            if (checkBox1.Checked)
            {
                ziList = GetString(zi.Split(Environment.NewLine.ToCharArray()));
            }
            else
            {
                ziList = GetString(zi.Split(Char.Parse(textBox8.Text)));
            }

            for (int i = 0; i < ziList.Count; i++)
            {
                int TopInt = 0;
                Label Label = new Label();
                Label.Text = ziList[i];
                Label.Name = "mylable" + i.ToString();
                Label.Tag = i.ToString();

                if (i == 0)
                {
                    TopInt = 5;
                }
                else
                {
                    TopInt = i * 60;
                }

                Label.Top = TopInt + 15;
                Label.Left = 2;


                ComboBox comboox = new ComboBox();
                TextBox TextBox = new TextBox();
                comboox.Tag = i.ToString();
                TextBox.Tag = i.ToString();
                TextBox.Name = "text" + i.ToString();
                TextBox.MaxLength = 327670;
                SetComboBox(comboox, TextBox);

                comboox.Top = TopInt + 15;
                comboox.Left = 110;

                //comboox.SelectedIndexChanged = "";
                comboox.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChangedMy);

                TextBox.Multiline = true;
                Size size = new Size();
                size.Width = 480;
                size.Height = 50;

                TextBox.Size = size;
                TextBox.Top = TopInt;
                TextBox.Left = 250;
                TextBox.DoubleClick += new EventHandler(button_MouseClick);

                panel1.Controls.Add(comboox);
                panel1.Controls.Add(Label);
                panel1.Controls.Add(TextBox);
            }
        }
        private void button_MouseClick(object sender, EventArgs e)
        {
            TextBox button = (TextBox)sender;
            button.Focus();
            button.SelectAll();
        }
        private void textBox3_DoubleClick(object sender, EventArgs e)
        {
            textBox3.Focus();
            textBox3.SelectAll();
        }

        private void textBox5_DoubleClick(object sender, EventArgs e)
        {
            textBox5.Focus();
            textBox5.SelectAll();
        }

        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.SelectAll();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabPage1;

            if (list.Count() == 0)
            {
                var newlist = GetString(textBox1.Text.Split(Environment.NewLine.ToCharArray()));
                foreach (var item in newlist)
                {
                    list.Add(item + "\r\n");
                }
            }

            label17.Text = "总："+list.Count();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                string kaishi = textBox2.Text;
                string jieshu = textBox4.Text;

                int hangshu = int.Parse(textBox6.Text);
                string jiange = textBox9.Text;

                string[] jiangelist = jiange.Split(' ');
                List<int> shuliang = new List<int>();

                for (int i = 0; i < jiangelist.Count(); i++)
                {
                    if (!string.IsNullOrEmpty(jiangelist[i]))
                    {
                        shuliang.Add(int.Parse(jiangelist[i]));
                    }
                }

                if (shuliang.Count() != hangshu)
                {
                    MessageBox.Show("行数和每行数目的行数不符!");
                    return;
                }

                if (list.Count() == 0)
                {
                    var newlist = GetString(textBox1.Text.Split(Environment.NewLine.ToCharArray()));
                    foreach (var item in newlist)
                    {
                        list.Add(item + "\r\n");
                    }
                }

                int csint = 0;
                string jieguo = "";
                string dan = "";

                for (int j = 0; j < hangshu; j++)
                {
                    string diejiakaishi = "";
                    string diejiajieshu = "";
                    if (shuliang[j] > 1) { diejiakaishi = textBox12.Text; diejiajieshu = textBox13.Text; }

                    jieguo += kaishi.Replace("[row]", (j + 1).ToString()).Replace("[rows]", shuliang[j].ToString());
                    dan = "";
                    for (int i = 0; i < shuliang[j]; i++)
                    {
                        dan += diejiakaishi+list[i + csint]+ diejiajieshu;
                    }
                    if (checkBox2.Checked)
                    {
                        string fuhao = textBox11.Text;
                        int ziwei = dan.LastIndexOf(fuhao);
                        if (ziwei != -1)
                        {
                            string zikai = dan.Substring(0, ziwei);
                            ziwei += fuhao.Length;
                            string zijie = dan.Substring(ziwei, dan.Length - ziwei);
                            dan = zikai + zijie;
                        }
                    }
                    jieguo += dan;
                    jieguo += jieshu;
                    csint += shuliang[j];

                }
                textBox10.Text = jieguo;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBox10.Text);
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            string jiange = textBox9.Text;

            string[] jiangelist = jiange.Split(' ');
            List<int> shuliang = new List<int>();
            
            for (int i = 0; i < jiangelist.Count(); i++)
            {
                if (!string.IsNullOrEmpty(jiangelist[i]))
                {
                    shuliang.Add(int.Parse(jiangelist[i]));
                }
            }

            label15.Text = "现：" + shuliang.Sum().ToString();

            textBox6.Text = shuliang.Count().ToString();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //if (textBox4.Focused)
            //{
            //    string val = "[row]";
            //    string zhi = textBox4.Text;
            //    int idx = textBox4.SelectionStart;
            //    zhi = zhi.Insert(idx, val);

            //    textBox4.Text = zhi;
            //    textBox4.SelectionStart = idx + val.Count();
            //    textBox4.Focus();
            //}
            //else if (textBox2.Focused)
            //{
                string val = "[row]";
                string zhi = textBox2.Text;
                int idx = textBox2.SelectionStart;
                zhi = zhi.Insert(idx, val);

                textBox2.Text = zhi;
                textBox2.SelectionStart = idx + val.Count();
                textBox2.Focus();
            //}
            
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //if (textBox4.Focused)
            //{
            //    string val = "[rows]";
            //    string zhi = textBox4.Text;
            //    int idx = textBox4.SelectionStart;
            //    zhi = zhi.Insert(idx, val);

            //    textBox4.Text = zhi;
            //    textBox4.SelectionStart = idx + val.Count();
            //    textBox4.Focus();
            //}
            //else if (textBox2.Focused)
            //{
                string val = "[rows]";
                string zhi = textBox2.Text;
                int idx = textBox2.SelectionStart;
                zhi = zhi.Insert(idx, val);

                textBox2.Text = zhi;
                textBox2.SelectionStart = idx + val.Count();
                textBox2.Focus();
            //}
        }

        private void textBox2_DoubleClick(object sender, EventArgs e)
        {
            textBox2.Focus();
            textBox2.SelectAll();
        }

        private void textBox4_DoubleClick(object sender, EventArgs e)
        {
            textBox4.Focus();
            textBox4.SelectAll();
        }

        private void textBox10_DoubleClick(object sender, EventArgs e)
        {
            textBox10.Focus();
            textBox10.SelectAll();
        }

        private void textBox9_DoubleClick(object sender, EventArgs e)
        {
            textBox9.Focus();
            textBox9.SelectAll();
        }

        private void textBox6_DoubleClick(object sender, EventArgs e)
        {
            textBox6.Focus();
            textBox6.SelectAll();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            list.Clear();
                   //if (list.Count() == 0)
                   //{
                   var newlist = GetString(textBox1.Text.Split(Environment.NewLine.ToCharArray()));
                foreach (var item in newlist)
                {
                    list.Add(item + "\r\n");
                }
            //}

            label17.Text = "总：" + list.Count();
        }
    }
}
